package client

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	product "product_server/proto"
)

type ProductHandler func(ctx context.Context, client product.ProductClient) (interface{}, error)

func ProductClient(ctx context.Context, handler ProductHandler) (interface{}, error) {
	dial, err := grpc.Dial(fmt.Sprintf("%s:%d", "127.0.0.1", 9995), grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := product.NewProductClient(dial)
	return handler(ctx, client)
}

func ProductList(ctx context.Context, in *product.ProductListRequest) (*product.ProductListResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.ProductList(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.ProductListResponse), nil
}

func ProductCreateCat(ctx context.Context, in *product.ProductCreateCatRequest) (*product.ProductCreateCatResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.ProductCreateCat(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.ProductCreateCatResponse), nil
}

func DelCarProduct(ctx context.Context, in *product.DelCarProductRequest) (*product.DelCarProductResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.DelCarProduct(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.DelCarProductResponse), nil
}

func UpdateCarProduct(ctx context.Context, in *product.UpdateCarProductRequest) (*product.UpdateCarProductResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.UpdateCarProduct(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.UpdateCarProductResponse), nil
}

func DelCar(ctx context.Context, in *product.DelCarRequest) (*product.DelCarResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.DelCar(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.DelCarResponse), nil
}

func CarList(ctx context.Context, in *product.CarListRequest) (*product.CarListResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.CarList(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.CarListResponse), nil
}

func CheckoutCar(ctx context.Context, in *product.CheckoutCarRequest) (*product.CheckoutCarResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.CheckoutCar(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.CheckoutCarResponse), nil
}

func SnappedSpikesProduct(ctx context.Context, in *product.SnappedSpikesProductRequest) (*product.SnappedSpikesProductResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.SnappedSpikesProduct(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.SnappedSpikesProductResponse), nil
}

func CreateGroup(ctx context.Context, in *product.CreateGroupRequest) (*product.CreateGroupResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.CreateGroup(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.CreateGroupResponse), nil
}

func GetGroupDetail(ctx context.Context, in *product.GetGroupDetailRequest) (*product.GetGroupDetailResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.GetGroupDetail(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.GetGroupDetailResponse), nil
}

func ParseTaoBao(ctx context.Context, in *product.ParseTaoBaoRequest) (*product.ParseTaoBaoResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.ParseTaoBao(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.ParseTaoBaoResponse), nil
}

func JoinGroup(ctx context.Context, in *product.JoinGroupRequest) (*product.JoinGroupResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.JoinGroup(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.JoinGroupResponse), nil
}

func StartBargain(ctx context.Context, in *product.StartBargainRequest) (*product.StartBargainResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.StartBargain(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.StartBargainResponse), nil
}

func HelpBargain(ctx context.Context, in *product.HelpBargainRequest) (*product.HelpBargainResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.HelpBargain(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.HelpBargainResponse), nil
}

func QueryBargainProgress(ctx context.Context, in *product.QueryBargainProgressRequest) (*product.QueryBargainProgressResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.QueryBargainProgress(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.QueryBargainProgressResponse), nil
}

func BargainSettlement(ctx context.Context, in *product.BargainSettlementRequest) (*product.BargainSettlementResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.BargainSettlement(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.BargainSettlementResponse), nil
}

func SpikesProductList(ctx context.Context, in *product.SpikesProductListRequest) (*product.SpikesProductListResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.SpikesProductList(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.SpikesProductListResponse), nil
}

func BargainProductList(ctx context.Context, in *product.BargainProductListRequest) (*product.BargainProductListResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.BargainProductList(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.BargainProductListResponse), nil
}

func ProductInfo(ctx context.Context, in *product.ProductInfoRequest) (*product.ProductInfoResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.ProductInfo(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.ProductInfoResponse), nil
}

func SpikesProductInfo(ctx context.Context, in *product.SpikesProductInfoRequest) (*product.SpikesProductInfoResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.SpikesProductInfo(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.SpikesProductInfoResponse), nil
}

func BargainProductInfo(ctx context.Context, in *product.BargainProductInfoRequest) (*product.BargainProductInfoResponse, error) {
	client, err := ProductClient(ctx, func(ctx context.Context, client product.ProductClient) (interface{}, error) {
		res, err := client.BargainProductInfo(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*product.BargainProductInfoResponse), nil
}
